package com.jsgt.greekfire.ui.dialog

import android.app.Dialog
import android.os.Bundle
import android.view.Gravity
import android.view.ViewGroup
import android.view.Window
import android.widget.ImageView
import android.widget.TextView
import com.fhx.core.base.BaseDialogFragment
import com.fhx.core.utils.QRCodeUtils
import com.fhx.core.utils.ScreenUtil
import com.fhx.core.utils.ToastUtils
import com.google.gson.Gson
import com.jsgt.greekfire.R
import com.jsgt.greekfire.common.util.ViewCapture
import com.jsgt.greekfire.dao.model.IdentityWallet
import com.jsgt.greekfire.dao.model.WalletBean
import org.jetbrains.anko.find

class ReceiveQRDialog() : BaseDialogFragment() {
    private var wallet: IdentityWallet? = null
    private val height by lazy { ScreenUtil.dip2px(requireContext(), 200f).toInt() }
    private val qrMap by lazy { HashMap<String, String>() }

    constructor(wallet: IdentityWallet?) : this() {
        arguments = Bundle()
        arguments?.putParcelable("wallet", wallet)
    }

    override fun getLayoutId(): Int {
        return R.layout.dialog_receive_qr
    }

    override fun getDialogStyle(): Int {
        return R.style.DialogTheme
    }

    override fun canCancel(): Boolean {
        return false
    }

    override fun setWindowAttributes(window: Window?) {
        window?.setWindowAnimations(R.style.alpha_anim_bottom)
        window?.setGravity(Gravity.CENTER)
        window?.setLayout(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT
        )
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        wallet = arguments?.getParcelable("wallet")
    }

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val dialog = super.onCreateDialog(savedInstanceState)

        dialog.find<TextView>(R.id.txvTitle).text =
            String.format(getString(R.string.receive_dialog_title), wallet?.key)
        dialog.find<TextView>(R.id.txvScanDesc).text =
            String.format(getString(R.string.receive_desc), wallet?.key)
        dialog.find<TextView>(R.id.txvAddress).text = wallet?.address

//        qrMap["coin"] = wallet?.key.toString()
//        qrMap["address"] = wallet?.address.toString()
        val codeBitmap = QRCodeUtils.createQRCodeBitmap(wallet?.address.toString(), height, height)
        dialog.find<ImageView>(R.id.imvQR).setImageBitmap(codeBitmap)

        dialog.find<TextView>(R.id.txvCancel).setOnClickListener {
            dismiss()
        }
        dialog.find<TextView>(R.id.txvOk).setOnClickListener {
            it.isEnabled = false
            val bitmap = ViewCapture.getViewBitmap(dialog.find(R.id.llContent))
            val isSuccess = ViewCapture.saveImageToGallery(requireContext(), bitmap)
            if (isSuccess) {
                ToastUtils.show(R.string.receive_save_success)
                dismiss()
            }else{
                ToastUtils.show(R.string.receive_save_failed)
                it.isEnabled = true
            }
        }
        return dialog
    }
}